<?php
/**
 * Created by PhpStorm.
 * User: hb
 * Date: 18-3-29
 * Time: 下午15:44
 */
namespace app\Logger;

use Monolog\Logger;
use Monolog\Handler\RotatingFileHandler;

use Server\CoreBase\CoreBase;


class WsdLogger extends CoreBase
{

    /**
     * WsdLogger constructor.
     * @param $channelName
     * @param $fileName
     */
    public function __construct($channelName, $fileName)
    {
        $this->logger = new Logger($channelName);
        $this->logger->pushHandler(new RotatingFileHandler(LOG_DIR . "/" . $fileName . '.log',
            $this->config['log']['logfile']['max_files']));
    }

    /**
     * INFO 日志记录
     * @param $message
     * @param array $context
     * @return mixed
     */
    public function info( $message, $context=array() )
    {
        if ( empty($context) ){
            $context = array();
        }

        // 日志数据脱敏处理
        $context = $this->maskingLogData($context);

        return $this->logger->info($message, $context);
    }

    /**
     * 日志数据脱敏处理
     * @param $logData
     * @return mixed
     */
    private function maskingLogData($logData) {
        // Add by hb on 20180329: 日志数据脱敏匹配关键字（密码, etc.）
        $maskingKeys = Array('password');

        foreach ($logData as $key => $value) {
            if (is_array($value)) {
                $value = $this->maskingLogData($value);
            }elseif ( in_array($key, $maskingKeys) ){
                $value = "******";
            }
            $logData[$key]=$value;
        }
        return $logData;
    }

}